package com.easepal802s.project.ui.presenter;


import android.util.Log;

import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.ui.iview.IAttitudeAdjustmentView;
import com.ogawa.base.utils.CommonUtil;

/**
 * Created by FHB on 2018/4/2.
 */

public class AttitudeAdjustmentPressenter extends MBasePresenter {
    IAttitudeAdjustmentView mView;
    private String THE_ATTITUDE ;
    public static String IS_RESET = "IS_RESET";//复位
    public static String IS_ZERO1 = "IS_ZERO1";//零重1
    public static String IS_ZERO2 = "IS_ZERO2";//零重2
    private int FLAG=-1 ;//调节姿态的哪个按键正在运行标志
    public static int UP_LEG = 0;//抬腿
    public static int DOWN_LEG = 1;//降腿
    public static int LONG_LEG = 2;//伸腿
    public static int SHORT_LEG = 3;//缩腿
    public static int DOWN_BACK = 4;//倒背
    public static int UP_BACK = 5;//抬背
    private byte mModelByte;//模式数据（复位 零重1或零重2）
    private byte mAttituteRunByte;//调节按钮运行数据


    public AttitudeAdjustmentPressenter(IAttitudeAdjustmentView view) {
        mView = view;
    }


    @Override
    public void getDatas(String data) {
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            //第一包数据
            byte[] buffer = HexUtils.hexToByte(data);
            //====buffer[3] ----对应block0.要扣除前面的7B(BYTE 0) B1(BYTE 1) OC(BYTE 2)
            //byte5  bit0 = 1 复位状态
            byte byte4 = buffer[4];
            if(mModelByte!=byte4){
                mModelByte = byte4;

                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,4,0))) {
                    THE_ATTITUDE = IS_RESET;

                } else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,4,1))){
                    THE_ATTITUDE = IS_ZERO1;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,4,2))){
                    THE_ATTITUDE = IS_ZERO2;
                }
                else {
                    THE_ATTITUDE ="none";
                }
                mView.getData(THE_ATTITUDE);
            }

            byte byte15 = buffer[15];
            if(mAttituteRunByte!=byte15) {
                Log.e("data15", String.valueOf(byte15));
                mAttituteRunByte = byte15;
                if (CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,0))) {
                    FLAG = UP_LEG;

                } else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,1))){
                    FLAG = DOWN_LEG;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,2))){
                    FLAG = LONG_LEG;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,3))){
                    FLAG = SHORT_LEG;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,4))){
                    FLAG = DOWN_BACK;
                }
                else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,15,5))){
                    FLAG = UP_BACK;
                }
                else {
                    FLAG =-1;
                }
                mView.adjustData(FLAG);
            }


        }


    }



}



